/**
 * Created by Administrator on 2017/6/2 0002.
 */
require(["jquery", "template", "bbcare", "scprit/department/view/template/addGrowingRecord.js"], function ($, template, $bb, $tpl) {
    var URL = $bb.baseUrl("department") + "followup/service"
    var userId
    //搜索:
    $("#toSearch").click(function () {
        var val = $("#search").val()
        if (val != "" || val.length != 0) {
            if (isNumber(val)) {
                searchTel(val)
            } else {
                searchName(val)
            }
        } else {
            $(".infoList>ul").html("请输入查询内容").removeClass("tipList hide").addClass("tipInfo")
        }
    })
    $("#search").on("input propertychange", function () {
        var val = $.trim($(this).val())
        if (val != "" || val.length != 0) {
            if (isNumber(val)) {
                searchTel(val)
            } else {
                searchName(val)
            }
        } else {
            $(".infoList>ul").html("")
        }
    })
    function searchTel (searchContent) {
        $bb.ajax({
            url: URL + "/patient/patientQueueListQuery",
            type: "post",
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            data: JSON.stringify({
                data: {
                    "flag": 2,
                    "phoneNum": searchContent  //18155163742;
                }
            }),
            success: function (data) {
                if (data.success) {
                    data = data.data.rows
                    if (data.length > 0) {
                        $(".infoList>ul").removeClass("tipInfo hide").addClass("tipList")
                        $tpl.infoInit2({ "id": "#infoLists", "data": data })
                    } else {
                        $(".infoList>ul").html("系统未找到该患者，请确保该患者已经建档").removeClass("tipList hide").addClass("tipInfo")
                    }
                }
            },
            error: function (data) { }
        })
    }
    function searchName (searchContent) {
        $bb.ajax({
            url: URL + "/patient/patientQueueListQuery",
            type: "post",
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            data: JSON.stringify({
                data: {
                    "flag": 2,
                    "patientName": searchContent  //墨渊;
                }
            }),
            success: function (data) {
                if (data.success) {
                    data = data.data.rows
                    if (data.length > 0) {
                        $(".infoList>ul").removeClass("tipInfo hide").addClass("tipList")
                        $tpl.infoInit2({ "id": "#infoLists", "data": data })
                    } else {
                        $(".infoList>ul").html("系统未找到该患者，请确保该患者已经建档").removeClass("tipList hide").addClass("tipInfo")
                    }
                }
            },
            error: function (data) { }
        })
    }

    $(".infoList").on("click", "li", function () {
        $(this).parent("ul").removeClass("tipInfo tipList").addClass("hide")
        var info = {}
        info.id = $(this).attr("userId")
        info.phone = $(this).attr("phone")
        info.sex = $(this).find(".sex").html()
        info.age = $(this).find(".age").html()
        info.userName = $(this).find(".userName").html()
        info.pregnancyWeek = $(this).find(".pregnancyWeek").html()
        info.followQueueName = $(this).find(".followQueueName").html()
        $tpl.infoInit({ "id": "#patientInfo", "data": info })
        userId = info.id
    })

    var flagWeight, flagHeight, flagHead
    function decimal_2 (element) {
        var val = $(element).val()
        val = val.replace(/[^\d.]/g, "")
        val = val.replace(/\.{2,}/g, ".")
        val = val.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".")
        val = val.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3')
        if (val.indexOf(".") < 0 && val != "") {
            val = parseFloat(val)
        }
        return val
    }
    function decimal_1 (element) {
        var val = $(element).val()
        val = val.replace(/[^\d.]/g, "")
        val = val.replace(/\.{1,}/g, ".")
        val = val.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".")
        val = val.replace(/^(\-)*(\d+)\.(\d).*$/, '$1$2.$3')
        if (val.indexOf(".") < 0 && val != "") {
            val = parseFloat(val)
        }
        return val
    }
    function decimal (element) {
        var val = $(element).val()
        val = val.replace(/[^\d]/g, '')
        return val
    }
    $('#weight_kg').on('input propertychange', function () {
        var val = decimal_2($(this))
        $(this).val(val)
        $('#weight_g').val(val ? parseInt(val * 1000) : '')
    })
    $('#weight_g').on('input propertychange', function () {
        var val = decimal($(this))
        $(this).val(val)
        $('#weight_kg').val(val ? val / 1000 : '')
    })


    $('#height_cm').on('input propertychange', function () {
        var val = decimal_1($(this))
        $(this).val(val)
        $('#height_mm').val(val ? parseInt(val * 10) : '')
    })
    $('#height_mm').on('input propertychange', function () {
        var val = decimal($(this))
        $(this).val(val)
        $('#height_cm').val(val ? val / 10 : '')
    })


    $('#head_cm').on('input propertychange', function () {
        var val = decimal_1($(this))
        $(this).val(val)
        $('#head_mm').val(val ? parseInt(val * 10) : '')
    })
    $('#head_mm').on('input propertychange', function () {
        var val = decimal($(this))
        $(this).val(val)
        $('#head_cm').val(val ? val / 10 : '')
    })

    /**
     * 清空
     */
    $('#clear').click(function () {
        $('#weight_kg').val('')
        $('#weight_g').val('')
        $('#height_cm').val('')
        $('#height_mm').val('')
        $('#head_cm').val('')
        $('#head_mm').val('')
    })



    //身高体重头围添加：
    $("#submit").click(function () {
        if (!userId) return $bb.toastShow("请先查询选择需要上传的用户！！！", 1500)

        if ($('#weight_g').val() === '') {
            return $bb.toastShow("请填写体重", 1500)
        } else if ($('#height_mm').val() === '') {
            return $bb.toastShow("请填写身高", 1500)
        } else if ($('#head_mm').val() === '') {
            return $bb.toastShow("请填写头围", 1500)
        }

        $bb.ajax({
            url: URL + "/patient/growth/addGrowthRecord",
            type: "post",
            contentType: "application/json;charset=UTF-8",
            dataType: "json",
            data: JSON.stringify({
                data: {
                    "userId": userId,
                    "height": String($('#height_mm').val() / 10),
                    "weight": String($('#weight_g').val() / 1000),
                    "headCircumference": String($('#head_mm').val() / 10)
                }
            }),
            success: function (data) {
                if (!data.success) return $bb.toastShow(data.msg, 1500)
                var growthRecordId = data.data.id
                var obj = {
                    "growthRecordId": growthRecordId,
                    "userId": userId
                }
                $bb.openPage("growthDevelopmentDetails", obj)
            },
            error: function (err) {
                $bb.toastShow("保存失败，请联系管理员", 1500)
            }
        })
    })

    function isNumber (obj) {
        var reg = /^[0-9]*$/
        return reg.test(obj)
    };

    //保留整数；
    function exp (obj) {
        var reg2 = /^\+?[1-9]\d*$/
        return reg2.test(obj)
    }
})